package cn.iocoder.yudao.module.member.controller.admin.majorobtain;

import cn.iocoder.yudao.module.member.service.major.MajorService;
import io.swagger.v3.oas.annotations.Parameters;
import org.springframework.web.bind.annotation.*;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import java.util.*;
import java.io.IOException;

import cn.iocoder.yudao.framework.common.pojo.PageParam;
import cn.iocoder.yudao.framework.common.pojo.PageResult;
import cn.iocoder.yudao.framework.common.pojo.CommonResult;
import cn.iocoder.yudao.framework.common.util.object.BeanUtils;
import static cn.iocoder.yudao.framework.common.pojo.CommonResult.success;

import cn.iocoder.yudao.framework.excel.core.util.ExcelUtils;

import cn.iocoder.yudao.framework.apilog.core.annotation.ApiAccessLog;
import static cn.iocoder.yudao.framework.apilog.core.enums.OperateTypeEnum.*;

import cn.iocoder.yudao.module.member.controller.admin.majorobtain.vo.*;
import cn.iocoder.yudao.module.member.dal.dataobject.majorobtain.MajorObtainDO;
import cn.iocoder.yudao.module.member.service.majorobtain.MajorObtainService;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

@Tag(name = "管理后台 - 专业表-就业方向")
@RestController
@RequestMapping("/member/major-obtain")
@Validated
public class MajorObtainController {

    @Resource
    private MajorObtainService majorObtainService;

    @Resource
    private MajorService majorService;

    @PostMapping("/create")
    @Operation(summary = "创建专业表-就业方向")
    public CommonResult<Long> createMajorObtain(@Valid @RequestBody MajorObtainSaveReqVO createReqVO) {
        return success(majorObtainService.createMajorObtain(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新专业表-就业方向")
    public CommonResult<Boolean> updateMajorObtain(@Valid @RequestBody MajorObtainSaveReqVO updateReqVO) {
        majorObtainService.updateMajorObtain(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除专业表-就业方向")
    @Parameter(name = "id", description = "编号", required = true)
    public CommonResult<Boolean> deleteMajorObtain(@RequestParam("id") Long id) {
        majorObtainService.deleteMajorObtain(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得专业表-就业方向")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    public CommonResult<MajorObtainRespVO> getMajorObtain(@RequestParam("id") Long id) {
        MajorObtainDO majorObtain = majorObtainService.getMajorObtain(id);
        return success(BeanUtils.toBean(majorObtain, MajorObtainRespVO.class));
    }

    @GetMapping("/page")
    @Operation(summary = "获得专业表-就业方向分页")
    public CommonResult<PageResult<MajorObtainRespVO>> getMajorObtainPage(@Valid MajorObtainPageReqVO pageReqVO) {
        PageResult<MajorObtainDO> pageResult = majorObtainService.getMajorObtainPage(pageReqVO);
        PageResult<MajorObtainRespVO> bean = BeanUtils.toBean(pageResult, MajorObtainRespVO.class);
        for (MajorObtainRespVO respVO: bean.getList()) {
            respVO.setMajorName(majorService.getMajor(respVO.getMajorId()).getName());
        }
        return success(bean);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出专业表-就业方向 Excel")
    @ApiAccessLog(operateType = EXPORT)
    public void exportMajorObtainExcel(@Valid MajorObtainPageReqVO pageReqVO,
              HttpServletResponse response) throws IOException {
        pageReqVO.setPageSize(PageParam.PAGE_SIZE_NONE);
        List<MajorObtainDO> list = majorObtainService.getMajorObtainPage(pageReqVO).getList();
        // 导出 Excel
        ExcelUtils.write(response, "专业表-就业方向.xls", "数据", MajorObtainRespVO.class,
                        BeanUtils.toBean(list, MajorObtainRespVO.class));
    }

    @PostMapping("/import")
    @Operation(summary = "导入用户")
    @Parameters({
            @Parameter(name = "file", description = "Excel 文件", required = true),
            @Parameter(name = "updateSupport", description = "是否支持更新，默认为 false", example = "true")
    })
    public void importExcel(@RequestParam("file") MultipartFile file) throws Exception {
        List<MajorObtainRespVO> list = ExcelUtils.read(file, MajorObtainRespVO.class);
        majorObtainService.importExcel(list);
    }

}